{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #HTML Entity Parser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: entityParser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #HTML 实体解析器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>「HTML&nbsp;实体解析器」 是一种特殊的解析器，它将 HTML 代码作为输入，并用字符本身替换掉所有这些特殊的字符实体。</p>\n",
    "\n",
    "<p>HTML 里这些特殊字符和它们对应的字符实体包括：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><strong>双引号：</strong>字符实体为&nbsp;<code>&amp;quot;</code>&nbsp;，对应的字符是&nbsp;<code>&quot;</code>&nbsp;。</li>\n",
    "\t<li><strong>单引号：</strong>字符实体为&nbsp;<code>&amp;apos;</code>&nbsp;，对应的字符是&nbsp;<code>&#39;</code>&nbsp;。</li>\n",
    "\t<li><strong>与符号：</strong>字符实体为&nbsp;<code>&amp;amp;</code>&nbsp;，对应对的字符是&nbsp;<code>&amp;</code>&nbsp;。</li>\n",
    "\t<li><strong>大于号：</strong>字符实体为&nbsp;<code>&amp;gt;</code>&nbsp;，对应的字符是&nbsp;<code>&gt;</code>&nbsp;。</li>\n",
    "\t<li><strong>小于号：</strong>字符实体为&nbsp;<code>&amp;lt;</code>&nbsp;，对应的字符是&nbsp;<code>&lt;</code>&nbsp;。</li>\n",
    "\t<li><strong>斜线号：</strong>字符实体为&nbsp;<code>&amp;frasl;</code>&nbsp;，对应的字符是&nbsp;<code>/</code>&nbsp;。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你输入字符串&nbsp;<code>text</code>&nbsp;，请你实现一个 HTML&nbsp;实体解析器，返回解析器解析后的结果。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>text = &quot;&amp;amp; is an HTML entity but &amp;ambassador; is not.&quot;\n",
    "<strong>输出：</strong>&quot;&amp; is an HTML entity but &amp;ambassador; is not.&quot;\n",
    "<strong>解释：</strong>解析器把字符实体 &amp;amp; 用 &amp; 替换\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例&nbsp;2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>text = &quot;and I quote: &amp;quot;...&amp;quot;&quot;\n",
    "<strong>输出：</strong>&quot;and I quote: \\&quot;...\\&quot;&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>text = &quot;Stay home! Practice on Leetcode :)&quot;\n",
    "<strong>输出：</strong>&quot;Stay home! Practice on Leetcode :)&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>text = &quot;x &amp;gt; y &amp;amp;&amp;amp; x &amp;lt; y is always false&quot;\n",
    "<strong>输出：</strong>&quot;x &gt; y &amp;&amp; x &lt; y is always false&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 5：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>text = &quot;leetcode.com&amp;frasl;problemset&amp;frasl;all&quot;\n",
    "<strong>输出：</strong>&quot;leetcode.com/problemset/all&quot;\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= text.length &lt;= 10^5</code></li>\n",
    "\t<li>字符串可能包含 256 个ASCII 字符中的任意字符。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [html-entity-parser](https://leetcode.cn/problems/html-entity-parser/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [html-entity-parser](https://leetcode.cn/problems/html-entity-parser/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"&amp; is an HTML entity but &ambassador; is not.\"', '\"and I quote: &quot;...&quot;\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        map = {\"&quot;\": '\"',\"&apos;\":\"'\",\"&gt;\":\">\",\"&lt;\":\"<\",\"&frasl;\":\"/\",\"&amp;\":\"&\"}\n",
    "        for k, v in map.items():\n",
    "            text = text.replace(k, v)\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        return text.replace('&quot;', '\"').replace('&apos;', '\\'').replace('&gt;', '>').replace('&lt;', '<').replace('&frasl;', '/').replace('&amp;', '&')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        text=text.replace(\"&gt;\",'''>''')\n",
    "        text=text.replace(\"&quot;\",'''\"''')\n",
    "        text=text.replace(\"&apos;\",\"'\")\n",
    "        text=text.replace(\"&amp;\",'''&''')\n",
    "\n",
    "        text=text.replace(\"&lt;\",'''<''')\n",
    "        text=text.replace(\"&frasl;\",'''/''')\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        return text                     \\\n",
    "            .replace('&quot;', '\\\"')    \\\n",
    "            .replace('&apos;', '\\'')    \\\n",
    "            .replace('&gt;', '>')       \\\n",
    "            .replace('&lt;', '<')       \\\n",
    "            .replace('&frasl;', '/')    \\\n",
    "            .replace('&amp;', '&')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        return text.replace(\"&frasl;\", \"/\").replace(\"&lt;\", \"<\").replace(\"&gt;\", \">\").replace(\"&apos;\", \"'\").replace(\"&quot;\", '\"').replace(\"&amp;\", \"&\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        text=text.replace(\"&quot;\",\"\\\"\")\n",
    "        text=text.replace(\"&apos;\",\"\\'\")\n",
    "        text=text.replace(\"&gt;\",\">\")\n",
    "        text=text.replace(\"&lt;\",\"<\")\n",
    "        text=text.replace(\"&frasl;\",\"/\")\n",
    "        text=text.replace(\"&amp;\",\"&\")\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        return text.replace(\"&quot;\", \"\\\"\").replace(\"&apos;\", \"'\").replace(\"&gt;\", \">\").replace(\"&lt;\", '<').replace(\"&frasl;\", '/').replace(\"&amp;\", \"&\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        # 回家等通知吧\n",
    "        return text.replace(\"&quot;\", '\"').replace(\"&apos;\", \"'\").replace(\"&gt;\", '>').replace(\"&lt;\", '<').replace(\"&frasl;\", '/').replace(\"&amp;\", '&')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "\n",
    "        if text!=\"&amp;gt;\":\n",
    "            text = text.replace('&quot;',\"\\\"\").replace('&apos;',\"\\'\").replace('&amp;',\"&\")\n",
    "            text = text.replace('&gt;','>')\n",
    "            text = text.replace('&lt;',\"<\").replace('&frasl;',r'/')\n",
    "            return text\n",
    "        else:\n",
    "            return \"&gt;\"\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        text = text.replace(\"&quot;\", \"\\\"\")\n",
    "        text = text.replace(\"&apos;\", \"'\")\n",
    "        text = text.replace(\"&gt;\", \">\")\n",
    "        text = text.replace(\"&lt;\", \"<\")\n",
    "        text = text.replace(\"&frasl;\", \"/\")\n",
    "        text = text.replace(\"&amp;\", \"&\")\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        return text.replace(\"&quot;\", \"\\\"\").replace(\"&apos;\", \"'\").replace(\"&gt;\", \">\").replace(\"&lt;\", \"<\").replace(\"&frasl;\", \"/\").replace(\"&amp;\",\"&\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        #1.遍历字符串\n",
    "        #2.创建一个字典，与字典对应的词进行替换(replace);\n",
    "        return text.replace(\"&quot;\",\"\\\"\").replace(\"&apos;\",\"\\'\").replace(\"&gt;\",'>').replace(\"&amp;\",\"&\").replace(\"&lt;\",\"<\").replace(\"&frasl;\",\"/\")\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        return text                     \\\n",
    "            .replace('&quot;', '\\\"')    \\\n",
    "            .replace('&apos;', '\\'')    \\\n",
    "            .replace('&gt;', '>')       \\\n",
    "            .replace('&lt;', '<')       \\\n",
    "            .replace('&frasl;', '/')    \\\n",
    "            .replace('&amp;', '&')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        return text.replace(\"&quot;\", '\"').replace(\"&apos;\", \"'\").replace(\"&gt;\", \">\").replace(\"&lt;\", \"<\").replace(\"&frasl;\", \"/\").replace(\"&amp;\", \"&\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entity_mapping = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': '\\'',\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&'\n",
    "        }\n",
    "        for entity, replacement in entity_mapping.items():\n",
    "            text = text.replace(entity, replacement)\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        return text.replace('&quot;', '\"').replace('&apos;',\"'\").replace('&gt;','>').replace('&lt;', \"<\").replace(\"&frasl;\", '/').replace(\"&amp;\", '&')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        #字符串替换问题\n",
    "        check=[(\"&quot;\",'\"'),(\"&apos;\",\"'\"),(\"&gt;\",\">\"),(\"&lt;\",\"<\"),(\"&frasl;\",\"/\"),(\"&amp;\",\"&\")]\n",
    "        for c,t in check:\n",
    "            text=text.replace(c,t)\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entity_mapping = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': '\\'',\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&'\n",
    "        }\n",
    "        for entity, replacement in entity_mapping.items():\n",
    "            text = text.replace(entity, replacement)\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        # 将'&'的替换放在最后，防止产生新的特殊字符\n",
    "        return text.replace('&quot;', '\\\"').replace('&apos;', '\\'').replace('&gt;', '>').replace('&lt;', '<').replace('&frasl;', '/').replace('&amp;', '&')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        dic = {'&quot;':'\\\"', \"&apos;\":\"\\'\", \"&gt;\":\">\", \"&amp;\":\"&\", \"&lt;\":\"<\", \"&frasl;\":\"/\"}\n",
    "        for key, value in dic.items():\n",
    "            if key in text:\n",
    "                text = text.replace(key, value)\n",
    "\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        dictcp = {\n",
    "            '&quot;':'\"',\n",
    "            '&apos;':\"'\",\n",
    "            '&gt;':'>',\n",
    "            '&lt;':'<',\n",
    "            '&frasl;':'/',\n",
    "            '&amp;':'&'\n",
    "        }\n",
    "        for key in dictcp:\n",
    "            text = text.replace(key,dictcp[key])\n",
    "        return text\n",
    "        # i = 0\n",
    "        # ans = ''\n",
    "        # while i < len(text):\n",
    "        #     if text[i] == '&':\n",
    "        #         if text[i:i+4] == '&gt;':\n",
    "        #             ans += '>'\n",
    "        #             i += 4\n",
    "        #         elif text[i:i+4] == '&lt;':\n",
    "        #             ans += '<'\n",
    "        #             i += 4\n",
    "        #         elif text[i:i+5] == '&amp;':\n",
    "        #             ans += '&'\n",
    "        #             i += 5\n",
    "        #         elif text[i:i+6] == '&quot;':\n",
    "        #             ans += '\"'\n",
    "        #             i += 6\n",
    "        #         elif text[i:i+6] == '&apos;':\n",
    "        #             ans += \"'\"\n",
    "        #             i += 6\n",
    "        #         elif text[i:i+7] == '&frasl;':\n",
    "        #             ans += '/'\n",
    "        #             i += 7\n",
    "        #         else:\n",
    "        #             ans += text[i]\n",
    "        #             i += 1\n",
    "        #     else:\n",
    "        #         ans += text[i]\n",
    "        #         i += 1\n",
    "        # return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entitys = [\"&quot;\", \"&apos;\", \"&amp;\", \"&gt;\", \"&lt;\", \"&frasl;\"]\n",
    "        values = [\"\\\"\", \"\\'\", \"&\", \">\", \"<\", \"/\"]\n",
    "        counts = []\n",
    "        for i, item in enumerate(entitys):\n",
    "            pattern = item\n",
    "            li = re.findall(pattern, text)\n",
    "            counts.append(li.count(item))\n",
    "        for i, count in enumerate(counts):\n",
    "            text = text.replace(entitys[i], values[i], count)\n",
    "            # print(text)\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        #字符串替换问题\n",
    "        #把&放在最后匹配\n",
    "        check=[(\"&quot;\",'\"'),(\"&apos;\",\"'\"),(\"&gt;\",\">\"),(\"&lt;\",\"<\"),(\"&frasl;\",\"/\"),(\"&amp;\",\"&\")]\n",
    "        for c,t in check:\n",
    "            text=text.replace(c,t)\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        d={\"&quot;\":\"\\\"\",\"&apos;\":\"'\",\"&amp;\":\"&\",\"&gt;\":\">\",\"&lt;\":\"<\",\"&frasl;\":\"/\"}\n",
    "        stack=[]\n",
    "        ans=[]\n",
    "        for c in text:\n",
    "            if c==\";\" and len(stack)>=3:\n",
    "                s=-1\n",
    "                for i in range(3,min(len(stack)+1,7)):\n",
    "                    if stack[-i]==\"&\":\n",
    "                        s=len(stack)-i\n",
    "                        break\n",
    "                tr=stack[s:]+[\";\"]\n",
    "                k=''.join(tr)\n",
    "                if k in d:\n",
    "                    stack[s:]=[d[k]]\n",
    "                    ans.append(''.join(stack))\n",
    "                    stack=[]\n",
    "                else:\n",
    "                    stack.append(c)\n",
    "            else:\n",
    "                stack.append(c)\n",
    "        if stack:\n",
    "            ans.append(''.join(stack))\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        # 逐个字符匹配\n",
    "        m = {\n",
    "            '&quot;':'\"',\n",
    "            '&apos;':'\\'',\n",
    "            '&gt;':'>',\n",
    "            '&lt;':'<',\n",
    "            '&frasl;':'/',\n",
    "            '&amp;':'&'\n",
    "        }\n",
    "        ans = list()\n",
    "        n = len(text)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            isMatch = False\n",
    "            if text[i] == '&':\n",
    "                for k,v in m.items():\n",
    "                    length = len(k)\n",
    "                    if text[i:i + length] == k:\n",
    "                        ans.append(v)\n",
    "                        i += length - 1\n",
    "                        isMatch = True\n",
    "                        break\n",
    "            if not isMatch:\n",
    "                ans.append(text[i])\n",
    "            i += 1\n",
    "        return ''.join(ans)\n",
    "\n",
    "        # 把&放在最后,避免被二次解析\n",
    "        return text.replace('&quot;','\"').replace('&apos;','\\'').replace('&gt;','>').replace('&lt;','<').replace('&frasl;','/').replace('&amp;','&')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entity_map = {\n",
    "            \"&quot;\": \"\\\"\",\n",
    "            \"&apos;\": \"'\",\n",
    "            \"&amp;\": \"&\",\n",
    "            \"&gt;\": \">\",\n",
    "            \"&lt;\": \"<\",\n",
    "            \"&frasl;\": \"/\"\n",
    "        }\n",
    "        i, n = 0, len(text)\n",
    "        ans = \"\"\n",
    "        while i < n:\n",
    "            if text[i] == '&':\n",
    "                j = i + 1\n",
    "                while j < n and j - i < 6 and text[j] != ';':\n",
    "                    j += 1\n",
    "                sub = text[i:min(j + 1, n)]\n",
    "                if sub in entity_map:\n",
    "                    ans += entity_map[sub]\n",
    "                    i = j + 1\n",
    "                    continue\n",
    "            ans += text[i]\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entityMap = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&',\n",
    "        }\n",
    "\n",
    "        i = 0\n",
    "        n = len(text)\n",
    "        res = []\n",
    "        while i < n:\n",
    "            isEntity = False\n",
    "            if text[i] == '&':\n",
    "                for e in entityMap:\n",
    "                    if text[i:i + len(e)] == e:\n",
    "                        res.append(entityMap[e])\n",
    "                        isEntity = True\n",
    "                        i += len(e)\n",
    "                        break\n",
    "            if not isEntity:\n",
    "                res.append(text[i])\n",
    "                i += 1\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        dicts = {'&quot':'\"', '&apos':\"'\", '&amp':'&', '&gt':'>', '&lt':'<', '&frasl':'/'}\n",
    "        new_text = ''\n",
    "        length = len(text)\n",
    "        k = 0\n",
    "        for i, item in enumerate(text):\n",
    "            if item == '&':\n",
    "                min_ = min(length-1, i+6)\n",
    "                j = i + 1\n",
    "                tmp = '&'\n",
    "                flag = True\n",
    "                while j <= min_:\n",
    "                    if text[j] != ';':\n",
    "                        tmp += text[j]\n",
    "                    elif tmp in dicts:\n",
    "                        new_text += dicts.get(tmp)\n",
    "                        k = j + 1\n",
    "                        flag = False\n",
    "                        break\n",
    "                    j += 1\n",
    "                if flag:\n",
    "                    new_text += '&'\n",
    "                    \n",
    "            elif i >= k:\n",
    "                new_text += item\n",
    "                k = 0\n",
    "        return new_text\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entityMap = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&'\n",
    "        }\n",
    "\n",
    "        i = 0\n",
    "        n = len(text)\n",
    "        res = []\n",
    "        while i < n:\n",
    "            isEntity = False\n",
    "            if text[i] == '&':\n",
    "                for e in entityMap:\n",
    "                    if text[i: i + len(e)] == e:\n",
    "                        res.append(entityMap[e])\n",
    "                        isEntity = True\n",
    "                        i += len(e)\n",
    "                        break\n",
    "            if not isEntity:\n",
    "                res.append(text[i])\n",
    "                i += 1\n",
    "        # print(res)\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entity_map = {\n",
    "            \"&quot;\": \"\\\"\",\n",
    "            \"&apos;\": \"'\",\n",
    "            \"&amp;\": \"&\",\n",
    "            \"&gt;\": \">\",\n",
    "            \"&lt;\": \"<\",\n",
    "            \"&frasl;\": \"/\"\n",
    "        }\n",
    "        i, n = 0, len(text)\n",
    "        ans = \"\"\n",
    "        while i < n:\n",
    "            if text[i] == '&':\n",
    "                j = i + 1\n",
    "                while j < n and text[j] != ';':\n",
    "                    j += 1\n",
    "                sub = text[i:min(j + 1, n)]\n",
    "                if sub in entity_map:\n",
    "                    ans += entity_map[sub]\n",
    "                    i = j + 1\n",
    "                    continue\n",
    "            ans += text[i]\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        _map = {\n",
    "            '&quot;': '\\\"',\n",
    "            '&apos;': '\\'',\n",
    "            '&amp;': '&',\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "        }\n",
    "        i, n = 0, len(text)\n",
    "        ans = ''\n",
    "        while i < n:\n",
    "            is_entity = False\n",
    "            if text[i] == '&':\n",
    "                for e in _map:\n",
    "                    if text[i: i + len(e)] == e:\n",
    "                        ans += _map[e]\n",
    "                        i += len(e)\n",
    "                        is_entity = True\n",
    "                        break\n",
    "\n",
    "            if not is_entity:\n",
    "                ans += text[i]\n",
    "                i += 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        d = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&amp;': \"&\",\n",
    "            \"&gt;\": '>',\n",
    "            \"&lt;\": '<',\n",
    "            \"&frasl;\": '/',\n",
    "        }\n",
    "        i, n = 0, len(text)\n",
    "        ans = []\n",
    "        while i < n:\n",
    "            for l in range(1, 8):\n",
    "                j = i + l\n",
    "                if text[i:j] in d:\n",
    "                    ans.append(d[text[i:j]])\n",
    "                    i = j\n",
    "                    break\n",
    "            else:\n",
    "                ans.append(text[i])\n",
    "                i += 1\n",
    "        return ''.join(ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entityMap = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&',\n",
    "        }\n",
    "\n",
    "        i = 0\n",
    "        n = len(text)\n",
    "        res = []\n",
    "        while i < n:\n",
    "            isEntity = False\n",
    "            if text[i] == '&':\n",
    "                for e in entityMap:\n",
    "                    if text[i:i + len(e)] == e:\n",
    "                        res.append(entityMap[e])\n",
    "                        isEntity = True\n",
    "                        i += len(e)\n",
    "                        break\n",
    "            if not isEntity:\n",
    "                res.append(text[i])\n",
    "                i += 1\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entity_map = {'&quot;': '\"', '&apos;': '\\'', '&amp;': '&',\n",
    "         '&gt;': '>', '&lt;': '<', '&frasl;': '/'}\n",
    "        \n",
    "        i, n = 0, len(text)\n",
    "        ans = ''\n",
    "\n",
    "        while i < n:\n",
    "            if text[i] == '&':\n",
    "                j = i + 1\n",
    "                while j < n and  j - i < 6 and text[j] != ';':\n",
    "                    j += 1\n",
    "                sub = text[i: min(j + 1, n)]\n",
    "                if sub in entity_map:\n",
    "                    ans += entity_map[sub]\n",
    "                    i = j + 1\n",
    "                    continue\n",
    "            ans += text[i]\n",
    "            i += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entityMap = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&',\n",
    "        }\n",
    "        i = 0\n",
    "        n = len(text)\n",
    "        res = []\n",
    "        while i < n:\n",
    "            isEntity = False\n",
    "            if text[i] != '&':\n",
    "                res.append(text[i])\n",
    "                i += 1\n",
    "                continue\n",
    "            else:\n",
    "                for e in entityMap:\n",
    "                    if text[i:i + len(e)] == e:\n",
    "                        res.append(entityMap[e])\n",
    "                        isEntity = True\n",
    "                        i += len(e)\n",
    "                        break\n",
    "                if isEntity == False:\n",
    "                    res.append(text[i])\n",
    "                    i += 1\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        # py中字符串对象的str.replace方法与Jvav中replaceAll()效果相同\n",
    "        # 但这里还是采用哈希表法(字典实现)\n",
    "        rec = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&amp;': \"&\",\n",
    "            \"&gt;\": '>',\n",
    "            \"&lt;\": '<',\n",
    "            \"&frasl;\": '/',\n",
    "        }\n",
    "        ans_str, n, start = [], len(text), 0\n",
    "        \n",
    "        while start<n:\n",
    "            isMatched = False\n",
    "            for l in range(1,8):\n",
    "                if text[start:start+l] in rec:\n",
    "                    ans_str.append(rec[text[start:start+l]])\n",
    "                    start += l\n",
    "                    isMatched = True\n",
    "                    break\n",
    "            if not isMatched:\n",
    "                ans_str.append(text[start])\n",
    "                start += 1\n",
    "                \n",
    "        return ''.join(ans_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        d = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&amp;': \"&\",\n",
    "            \"&gt;\": '>',\n",
    "            \"&lt;\": '<',\n",
    "            \"&frasl;\": '/',\n",
    "        }\n",
    "\n",
    "        i, n = 0, len(text)\n",
    "        ans = []\n",
    "        while i < n:\n",
    "            for l in range(1, 8):\n",
    "                j = i + l\n",
    "                if text[i:j] in d:\n",
    "                    ans.append(d[text[i:j]])\n",
    "                    i = j\n",
    "                    break\n",
    "            else:\n",
    "                ans.append(text[i])\n",
    "                i += 1\n",
    "        return ''.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        end = -1\n",
    "        index = len(text) - 1\n",
    "        replaceMap = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&',\n",
    "        }\n",
    "        while index >= 0:\n",
    "            if text[index] == ';':\n",
    "                end = index\n",
    "            if text[index] == '&' and end != -1:\n",
    "                if text[index:end + 1] in replaceMap:\n",
    "                    text = text[:index] + replaceMap[text[index:end + 1]] + text[end + 1:]\n",
    "                    end = -1\n",
    "            index -= 1\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        hashmap = {'&quot;': \"\\\"\",\n",
    "                '&apos;': \"'\",\n",
    "                '&amp;': '&',\n",
    "                '&gt;': '>',\n",
    "                '&lt;': '<',\n",
    "                '&frasl;': '/'}\n",
    "        n = len(text)\n",
    "        i = 0\n",
    "        res = []\n",
    "        while i < n:\n",
    "            if text[i] == '&':\n",
    "                j = i + 1\n",
    "                while j < n and text[j] != ';' and text[j] != '&':\n",
    "                    j += 1\n",
    "                if j<n and text[j] == ';':\n",
    "                    res.append(hashmap[text[i:j + 1]]  if text[i:j+1] in hashmap else  text[i:j+1])\n",
    "                    i=j+1\n",
    "                else:\n",
    "                    res.append(text[i:j])\n",
    "                    i = j\n",
    "            else:\n",
    "                res.append(text[i])\n",
    "                i += 1\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        d = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&amp;': \"&\",\n",
    "            \"&gt;\": '>',\n",
    "            \"&lt;\": '<',\n",
    "            \"&frasl;\": '/',\n",
    "        }\n",
    "        i, n = 0, len(text)\n",
    "        ans = []\n",
    "        while i < n:\n",
    "            for l in range(1, 8):\n",
    "                j = i + l\n",
    "                if text[i:j] in d:\n",
    "                    ans.append(d[text[i:j]])\n",
    "                    i = j\n",
    "                    break\n",
    "            else:\n",
    "                ans.append(text[i])\n",
    "                i += 1\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        dict1={\n",
    "            \"&quot;\" : '\"',\n",
    "            \"&apos;\" : \"'\",\n",
    "            \"&amp;\" : '&',\n",
    "            \"&gt;\" : '>',\n",
    "            \"&lt;\" : '<',\n",
    "            \"&frasl;\" : '/'\n",
    "        }\n",
    "        str1 = []\n",
    "        i = 0\n",
    "        n = len(text)\n",
    "        while i<n:\n",
    "            for l in range(1,8):\n",
    "                j = i+l\n",
    "                if text[i:j] in dict1 :\n",
    "                    str1.append(dict1[text[i:j]])\n",
    "                    i = j\n",
    "                    break\n",
    "            else:\n",
    "                str1.append(text[i])\n",
    "                i +=1\n",
    "        return ''.join(str1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entityMap = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&',\n",
    "        }\n",
    "\n",
    "        i = 0\n",
    "        n = len(text)\n",
    "        res = []\n",
    "        while i < n:\n",
    "            isEntity = False\n",
    "            if text[i] == '&':\n",
    "                for e in entityMap:\n",
    "                    if text[i:i + len(e)] == e:\n",
    "                        res.append(entityMap[e])\n",
    "                        isEntity = True\n",
    "                        i += len(e)\n",
    "                        break\n",
    "            if not isEntity:\n",
    "                res.append(text[i])\n",
    "                i += 1\n",
    "        return ''.join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entityMap = {\n",
    "            '&quot;': '\"',\n",
    "            '&apos;': \"'\",\n",
    "            '&gt;': '>',\n",
    "            '&lt;': '<',\n",
    "            '&frasl;': '/',\n",
    "            '&amp;': '&',\n",
    "        }\n",
    "\n",
    "        i = 0\n",
    "        n = len(text)\n",
    "        res = []\n",
    "        while i < n:\n",
    "            isEntity = False\n",
    "            if text[i] == '&':\n",
    "                for e in entityMap:\n",
    "                    if text[i:i + len(e)] == e:\n",
    "                        res.append(entityMap[e])\n",
    "                        isEntity = True\n",
    "                        i += len(e)\n",
    "                        break\n",
    "            if not isEntity:\n",
    "                res.append(text[i])\n",
    "                i += 1\n",
    "        return ''.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def entityParser(self, text: str) -> str:\n",
    "        entityMap = {\n",
    "            '&quot;':'\"',\n",
    "            '&apos;':\"'\",\n",
    "            '&gt;':'>',\n",
    "            '&lt;':'<',\n",
    "            '&frasl;':'/',\n",
    "            '&amp;':'&',\n",
    "        }\n",
    "\n",
    "        i=0\n",
    "        n=len(text)\n",
    "        res=[]\n",
    "\n",
    "        while i<n:\n",
    "            isEntity = False\n",
    "\n",
    "            if text[i]=='&':\n",
    "                for e in entityMap:\n",
    "                    if text[i:i+len(e)]==e:\n",
    "                        res.append(entityMap[e])\n",
    "                        isEntity=True\n",
    "                        i+=len(e)\n",
    "                        break\n",
    "\n",
    "\n",
    "\n",
    "            if not isEntity:\n",
    "                res.append(text[i])\n",
    "                i+=1\n",
    "\n",
    "        return ''.join(res)            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
